package com.neu.api.controller.admin;


import com.neu.api.constan.StatusConst;
import com.neu.api.dto.FoodDTO;
import com.neu.api.exception.ParamException;
import com.neu.api.query.FoodQuery;
import com.neu.api.service.FoodService;
import com.neu.api.utils.PageResult;
import com.neu.api.utils.Result;
import com.neu.api.vo.FoodVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Api(tags = "菜品接口")
@RestController
@RequestMapping("admin/food")
@Slf4j
public class FoodController {

    @Resource
    FoodService foodService;


    @ApiOperation("查询菜品接口")
    @GetMapping
    public Result<List<FoodVO>> find(FoodQuery foodQuery){
        log.info("查询菜品");
        PageResult<FoodVO> page = foodService.find(foodQuery);
        return Result.ok("成功",page.getList(),page.getTotal());
    }

    @ApiOperation("根据id查询菜品")
    @GetMapping("/{id}")
    public Result findById(@PathVariable long id){
        FoodVO foodVO = foodService.findById(id);
        return Result.ok("查询成功",foodVO);
    }
    /**
     * 添加菜品信息
     * @param foodDTO
     * @return
     */
    @ApiOperation("添加菜品接口")
    @PostMapping
    public Result add(FoodDTO foodDTO){
        log.info("添加信息参数为："+foodDTO.toString());
        foodService.add(foodDTO);
        return Result.ok("添加成功");
    }

    /**
     * 编辑菜品信息
     * @param id
     * @param foodDTO
     * @return
     */
    @ApiOperation("编辑菜品")
    @PutMapping("/{id}")
    public Result update(@PathVariable long id, FoodDTO foodDTO){
        log.info("编辑当前菜品id为："+id+",参数为："+foodDTO.toString());
        foodService.update(id,foodDTO);
        return Result.ok("修改成功！");
    }

    /**
     * 根据ID删除菜品
     * @param id
     * @return
     */
    @ApiOperation("根据ID删除菜品")
    @DeleteMapping("/{id}")
    public Result deleteById(@PathVariable long id){
        //TODO
        log.info("删除菜品的id位："+id);
        foodService.deleteById(id);
        return Result.ok("删除成功！");
    }

    /**
     * 启用禁用账号
     * @param id
     * @return
     */
    @ApiOperation("禁用启动菜品")
    @PutMapping("/{id}/status")
    public Result updateStatus(@PathVariable long id,String status){
        if(StatusConst.INACTIVE_STATUS_NAME.equals(status)){
            foodService.updateStatus(id,StatusConst.INACTIVE_STATUS);
        }else if(StatusConst.ACTIVE_STATUS_NAME.equals(status)){
            foodService.updateStatus(id,StatusConst.ACTIVE_STATUS);
        }else{
            throw new ParamException("参数错误");
        }
        return Result.ok("修改状态成功");
    }


}
